﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SES.CMS.DO;
using SES.CMS.BL;

namespace SES.CMS.Module
{
    public partial class ucBreadcrumb : System.Web.UI.UserControl
    {
        cmsLoaiSanPhamBL objLoaiSanPhamBL = new cmsLoaiSanPhamBL();
        cmsLoaiSanPhamDO objLoaiSanPhamDO = new cmsLoaiSanPhamDO();
        protected void Page_Load(object sender, EventArgs e)
        {
           

            string url = Request.Url.AbsolutePath;
            url = url.Substring(1, url.Length - 1);
            string url1 = url.Replace(".", "/");
            string Module = url1.Substring(0, url1.IndexOf("/"));
            if (Module == "SanPham")
            {
                int loaiSanPhamID = int.Parse(Request.QueryString["ID"].ToString());
                    lblTitle.Text = GetBreadcrumb(loaiSanPhamID);
                    DataTable dtLoaiSanPham = new cmsLoaiSanPhamBL().SelectByPK(loaiSanPhamID);
                    if (Session["lang"].ToString() == "VN")
                    {
                        lblTitle.Text += "<a href='/SanPham/" + loaiSanPhamID + "/" + FriendlyUrl(dtLoaiSanPham.Rows[0]["TenLoaiSanPham"].ToString()) + "'>" + dtLoaiSanPham.Rows[0]["TenLoaiSanPham"].ToString() + "</a>";
                    }
                    if (Session["lang"].ToString() == "EN")
                    {
                        lblTitle.Text += "<a href='/SanPham/" + loaiSanPhamID + "/" + FriendlyUrl(dtLoaiSanPham.Rows[0]["ProductTypeName"].ToString()) + "'>" + dtLoaiSanPham.Rows[0]["ProductTypeName"].ToString() + "</a>";
                    }
            }
            else if (Module == "Search")
            {
                if (Session["lang"].ToString() == "VN")
                {
                    lblTitle.Text = "<a href='/Search.aspx'>Tìm kiếm</a>";
                }
                if (Session["lang"].ToString() == "EN")
                {
                    lblTitle.Text = "<a href='/Search.aspx'>Search</a>";
                }
            }
            else if (Module == "About")
            {
                if (Request.QueryString["ID"] != null)
                {
                    int ID = int.Parse(Request.QueryString["ID"]);
                    string title = new cmsCategoryBL().Select(new cmsCategoryDO { CategoryID = ID }).Title;

                    lblTitle.Text = "<a href='/About/" + ID + "/" + FriendlyUrl(title) + "'>" + title + "</a>";
                }
            }

            else if (Module == "SanPhamChiTiet")
            {
                int sanPhamID = int.Parse(Request.QueryString["ID"].ToString());
                DataTable dtSanPham = new cmsSanPhamBL().SelectBySanPhamID(sanPhamID);
                if (dtSanPham.Rows.Count > 0)
                {
                    int loaiSanPhamID = int.Parse(dtSanPham.Rows[0]["LoaiSanPhamID"].ToString());
                    DataTable dtLoaiSanPham = new cmsLoaiSanPhamBL().SelectByPK(loaiSanPhamID);
                    lblTitle.Text = GetBreadcrumb(loaiSanPhamID);
                    if (dtLoaiSanPham.Rows.Count > 0)
                    {
                        if (Session["lang"].ToString() == "VN")
                        {
                            lblTitle.Text += "<a href='/SanPham/" + loaiSanPhamID + "/" + FriendlyUrl(dtLoaiSanPham.Rows[0]["TenLoaiSanPham"].ToString()) + "'>" + dtLoaiSanPham.Rows[0]["TenLoaiSanPham"].ToString() + "</a>";
                        }
                        if (Session["lang"].ToString() == "EN")
                        {
                            lblTitle.Text += "<a href='/SanPham/" + loaiSanPhamID + "/" + FriendlyUrl(dtLoaiSanPham.Rows[0]["ProductTypeName"].ToString()) + "'>" + dtLoaiSanPham.Rows[0]["ProductTypeName"].ToString() + "</a>";
                        }
                    }
                }

            }
            else if (Module == "Article")
            {
                lblTitle.Text = "<a href='/Category/3/Tin-Tuc.aspx'> Tin tức</a>";
            }
            else if (Module == "Contact")
                lblTitle.Text = "<a href='/Contact.aspx'> Liên Hệ</a>";
            else if (Module == "ThongTin")
                lblTitle.Text = "<a href='/ThongTin/" + Session["IDDN"].ToString() + "/Thong-tin-ca-nhan.aspx'> Thông tin cá nhân</a>";
            else if (Module == "DoiMK")
                lblTitle.Text = "<a href='/DoiMK.aspx'>Đổi mật khẩu</a>";
            else if (Module == "GioHang")
            {
                lblTitle.Text = "<a href='/GioHang.aspx'> Giỏ hàng </a>";
            }
            else if (Module == "Register")
            {
                lblTitle.Text = "<a href='/Register.aspx'> Đăng ký thành viên </a>";
            }
            else if (Module == "ThanhToan")
            {
                lblTitle.Text = "<a href='/ThanhToan.aspx'> Gửi đơn hàng </a>";
            }
            else
            {
                lblTitle.Text = "<a href='/" + Module + ".aspx'>" + Module + " </a>";
            }
        }

        protected string GetBreadcrumb(int loaiSanPhamID)
        {
            DataTable dtParentID = objLoaiSanPhamBL.SelectParentID(loaiSanPhamID);

            string[] a = new string[dtParentID.Rows.Count];

            string sBreadcrumb = "";

            for (int i = 0; i < dtParentID.Rows.Count - 1; i++)
            {
                objLoaiSanPhamDO.LoaiSanPhamID = int.Parse(dtParentID.Rows[i]["ParentID"].ToString());

                DataTable dtParent = objLoaiSanPhamBL.SelectOne(objLoaiSanPhamDO);
                if (Session["lang"].ToString() == "VN")
                {
                    a[i] = "<a href='/SanPham/" + dtParentID.Rows[i]["ParentID"] + "/" + FriendlyUrl(dtParent.Rows[0]["TenLoaiSanPham"].ToString()) + "'>" + dtParent.Rows[0]["TenLoaiSanPham"].ToString() + "</a>";
                }
                else
                {
                    a[i] = "<a href='/SanPham/" + dtParentID.Rows[i]["ParentID"] + "/" + FriendlyUrl(dtParent.Rows[0]["ProductTypeName"].ToString()) + "'>" + dtParent.Rows[0]["ProductTypeName"].ToString() + "</a>";
                }
            }

            for (int i = a.Length - 1; i >= 0; i--)
            {
                sBreadcrumb += a[i];
            }
            return sBreadcrumb;

        }

        protected string FriendlyUrl(string strTitle)
        {
            return Ultility.Change_AV(strTitle);
        }
        protected int GetParentID(int loaiSanPhamID)
        {
            if (loaiSanPhamID == 0)
                return loaiSanPhamID;
            else
                return objLoaiSanPhamBL.GetParentID(loaiSanPhamID);
        }
    }
}